//
//  HDArchiveHeaderView.swift
//  HuaDaoPeifu
//
//  Created by 刘汉琳 on 16/5/18.
//  Copyright © 2016年 paiyipai. All rights reserved.
//

import UIKit
protocol HDArchiveHeaderViewDelegate:class  {
    func btnClickDelegate()
    func noticeImageClick()
}

class HDArchiveHeaderView: UIView {
    
    weak var  delegate:HDArchiveHeaderViewDelegate?
    var viewHeight_Y:CGFloat = 0
    var archiveModel = HDArchivesModel()
    var showBtn:UIButton = UIButton()

    func setupHeaderView(){
        let payIconImageView = UIImageView()
        self.addSubview(payIconImageView)
        payIconImageView.snp.makeConstraints { (make) in
            make.top.equalTo(20)
            make.left.equalTo(15 - 2)
            make.height.equalTo(11)
            make.width.equalTo(11)
        }
        payIconImageView.image = UIImage(named: "赔付申请")
        
        let nameLabel = UILabel()
        self.addSubview(nameLabel)
        nameLabel.snp.makeConstraints { (make) in
            make.top.equalTo(17)
            make.left.equalTo(payIconImageView.snp.right).offset(5)
            make.width.equalTo(AppConstant.screenWidth - 40)
            make.height.equalTo(18)
        }
        nameLabel.text = "已结案"
        nameLabel.textAlignment = .left
        nameLabel.font = UIFont.systemFont(ofSize: 18)
        nameLabel.textColor = ConstantColor.appThemeColor
        viewHeight_Y += 45
        if archiveModel.is_dishonor.int32Value == 1 {
            createLabelWithIconImage("已给付，详情见赔案通知书", nameLable_Y: viewHeight_Y)

        }else if archiveModel.is_dishonor.int32Value == 2{
            createLabelWithIconImage("已拒付，详情见赔案通知书", nameLable_Y: viewHeight_Y)

        }else{
            createLabelWithIconImage("已撤单，详情查看撤单原因", nameLable_Y: viewHeight_Y)

        }
        viewHeight_Y += 20
        let perTime = archiveModel.closing_time.int32Value - archiveModel.upload_time.int32Value
        let timeText = "预赔付时效：" + Int(perTime).archiveDate()
        
        createLabelWithIconImage(timeText, nameLable_Y: viewHeight_Y)
        viewHeight_Y += 20
        
        if archiveModel.telephone.characters.count>0 {
            createLabelWithIconImage("申诉电话：\(self.archiveModel.telephone)", nameLable_Y: viewHeight_Y)
            viewHeight_Y += 20

        }
        viewHeight_Y += 18
        
        if archiveModel.is_dishonor.int32Value == 0 {
            createLabelWithIconImageView("撤单原因", nameLable_Y: viewHeight_Y, imagename: "撤单")
            viewHeight_Y += 30
            
            let dishonorLabel = UILabel()
            self.addSubview(dishonorLabel)
            dishonorLabel.text = archiveModel.dishonor_introduce
            let temp = dishonorLabel.text?.getTextRectSize(dishonorLabel.text! as NSString, font: UIFont.systemFont(ofSize: 13), size: CGSize(width: AppConstant.screenWidth - 60, height: 1000))
            dishonorLabel.snp.makeConstraints { (make) in
                make.top.equalTo(viewHeight_Y + 1)
                make.left.equalTo(32)
                make.width.equalTo(AppConstant.screenWidth - 60)
                make.height.equalTo((temp?.height)! + 5)
            }
            dishonorLabel.font = UIFont.systemFont(ofSize: 13)
            dishonorLabel.numberOfLines = 0
            dishonorLabel.textColor = UIColor(hex:0x666666)
            dishonorLabel.textAlignment = .left
            viewHeight_Y += (temp?.height)! + 18
            
        }else{
            createLabelWithIconImageView("赔案通知书", nameLable_Y: viewHeight_Y, imagename: "icon赔案通知书")
            viewHeight_Y += 30
            
            let noticeImageView = UIImageView()
            addSubview(noticeImageView)
            noticeImageView.snp.makeConstraints { (make) in
                make.top.equalTo(viewHeight_Y)
                make.left.equalTo(55)
                make.width.equalTo(62)
                make.height.equalTo(73)
            }
            noticeImageView.image = UIImage(named: "缩略图")
            noticeImageView.isUserInteractionEnabled = true
            viewHeight_Y += 18 + 73
            let tapGesture = UITapGestureRecognizer(target: self,action: #selector(HDArchiveHeaderView.noticeViewTap(_:)))
            noticeImageView.addGestureRecognizer(tapGesture)
        }
        
        if self.archiveModel.sourceflag.int32Value != 1 {
            let classifyImageView = UIImageView()
            self.addSubview(classifyImageView)
            classifyImageView.snp.makeConstraints { (make) in
                make.top.equalTo(viewHeight_Y)
                make.left.equalTo(32)
                make.height.equalTo(19)
                make.width.equalTo(19)
            }
            classifyImageView.image = UIImage(named: "资料分类_icon")
            classifyImageView.isUserInteractionEnabled = true
            let classifyLabel = UILabel()
            self.addSubview(classifyLabel)
            classifyLabel.snp.makeConstraints { (make) in
                make.top.equalTo(viewHeight_Y - 10)
                make.left.equalTo(10 + 39 + 10)
                make.width.equalTo(AppConstant.screenWidth - 40)
                make.height.equalTo(40)
            }
            classifyLabel.text = "资料分类"
            classifyLabel.textAlignment = .left
            classifyLabel.font = UIFont.systemFont(ofSize: 16)
            classifyLabel.textColor = UIColor(hex:0x333333)
            classifyLabel.isUserInteractionEnabled = true
            
            
            let labelTapGesture = UITapGestureRecognizer(target: self,action: #selector(HDArchiveHeaderView.tapClick))
            classifyLabel.addGestureRecognizer(labelTapGesture)
            addSubview(showBtn)
            showBtn.snp.makeConstraints { (make) in
                make.top.equalTo(viewHeight_Y)
                make.left.equalTo(AppConstant.screenWidth - 45)
                make.width.equalTo(30)
                make.height.equalTo(30)
            }
            
            showBtn.backgroundColor = UIColor.white
            showBtn.isSelected = false
            showBtn.addTarget(self, action:#selector(HDArchiveHeaderView.btnClick(_:)), for: .touchUpInside)
            
            showBtn.setImage(UIImage(named: "下拉展开_icon"), for: UIControlState())
            ///
            viewHeight_Y += 20 + 18
        
            
        }
        createLine()

        
        self.frame = CGRect(x: 0, y: 0, width: AppConstant.screenWidth, height: viewHeight_Y)
    }
    
    func createLabelWithIconImage(_ text:String,nameLable_Y:CGFloat){
        
        let payIconImageView = UIImageView()
        self.addSubview(payIconImageView)
        payIconImageView.snp.makeConstraints { (make) in
            make.top.equalTo(nameLable_Y + 3)
            make.left.equalTo( 32)
            make.height.equalTo(7)
            make.width.equalTo(7)
        }
        payIconImageView.image = UIImage(named: "圆角矩形-1-拷贝-2") //
        
        let nameLabel = UILabel()
        self.addSubview(nameLabel)
        nameLabel.snp.makeConstraints { (make) in
            make.top.equalTo(nameLable_Y)
            make.left.equalTo(payIconImageView.snp.right).offset(6)
            make.width.equalTo(AppConstant.screenWidth - 40)
            make.height.equalTo(13)
        }
        nameLabel.text = text
        nameLabel.textAlignment = .left
        nameLabel.font = UIFont.systemFont(ofSize: 13)
        
        nameLabel.textColor = UIColor(hex:0x666666)
    }

    func createLabelWithIconImageView(_ text:String,nameLable_Y:CGFloat,imagename:String){
        
        let payIconImageView = UIImageView()
        self.addSubview(payIconImageView)
        payIconImageView.snp.makeConstraints { (make) in
            make.top.equalTo(nameLable_Y)
            make.left.equalTo(32)
            make.height.equalTo(19)
            make.width.equalTo(19)
        }
        payIconImageView.image = UIImage(named: imagename) //
        
        let nameLabel = UILabel()
        self.addSubview(nameLabel)
        nameLabel.snp.makeConstraints { (make) in
            make.top.equalTo(nameLable_Y + 1)
            make.left.equalTo(payIconImageView.snp.right).offset(5)
            make.width.equalTo(AppConstant.screenWidth - 40)
            make.height.equalTo(16)
        }
        nameLabel.text = text
        nameLabel.textAlignment = .left
        nameLabel.font = UIFont.systemFont(ofSize: 16)
        
        nameLabel.textColor = UIColor(hex:0x333333)
    }
    
    func createLine(){
        let lineLabel = UILabel()
        self.addSubview(lineLabel)
        lineLabel.snp.makeConstraints { (make) in
            make.top.equalTo(30)
            make.left.equalTo(18)
            make.width.equalTo(1)
            make.bottom.equalTo(0)
        }
        lineLabel.backgroundColor = UIColor(hex:0xd5d5d5)
    }

    func btnClick(_ sender:UIButton){
        if sender.isSelected {
            showBtn.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI*2))
            showBtn.isSelected = false
            sender.isSelected = false
        }else{
            showBtn.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI))
            showBtn.isSelected = true
            sender.isSelected = true
        }
        delegate?.btnClickDelegate()
    }
    
    func tapClick(){
        if showBtn.isSelected == false{
            showBtn.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI))
            showBtn.isSelected = true
        }else{
            showBtn.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI*2))
            showBtn.isSelected = false
        }
        delegate?.btnClickDelegate()
    }
    
    func noticeViewTap(_ gesture:UITapGestureRecognizer){
        delegate?.noticeImageClick()
    }


}
